package com.hhkj.admin.course.service.biz.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hhkj.admin.course.service.base.CoursePriceConfService;
import com.hhkj.admin.course.service.extend.CoursePriceConfExtendService;
import com.hhkj.admin.course.domain.CoursePriceConf;
import com.hhkj.admin.course.model.dopo.CoursePriceConfDo;
import com.hhkj.admin.course.model.dopo.CoursePriceConfPo;
import com.hhkj.admin.course.model.req.CoursePriceConfReq;
import com.hhkj.admin.course.model.res.CoursePriceConfRes;
import com.hhkj.admin.course.service.biz.CoursePriceConfBizService;
import com.ruoyi.common.base.biz.service.biz.BaseBizServiceImpl;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CoursePriceConfBizServiceImpl extends BaseBizServiceImpl<CoursePriceConf, CoursePriceConfReq, CoursePriceConfRes, CoursePriceConfDo, CoursePriceConfPo, CoursePriceConfService, CoursePriceConfExtendService> implements CoursePriceConfBizService {

    @Override
    protected void beforeRemove(List<Long> idList) {
        // 不允许让用户删除掉所有的价格，至少要留一个
        List<Long> customerIdList = CollectionUtil.removeNull(idList);
        Long id = CollectionUtil.getFirst(idList);
        CoursePriceConf coursePriceConf = this.getService().getById(id);
        if (ObjectUtil.isEmpty(coursePriceConf)) {
            throw new GlobalException("数据不存在");
        }
        LambdaQueryWrapper<CoursePriceConf> queryWrapper = new LambdaQueryWrapper<CoursePriceConf>()
                .eq(CoursePriceConf::getCourseId, coursePriceConf.getCourseId());
        long count = this.getService().count(queryWrapper);
        if (customerIdList.size() >= count) {
            throw new GlobalException("不允许将所有价格全部删除");
        }
        super.beforeRemove(customerIdList);
    }
}
